package com.vehicle_information_management_system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle_information_management_system.dto.TripDto;
import com.vehicle_information_management_system.dto.VehicleCount;
import com.vehicle_information_management_system.dto.VehicleDto;
import com.vehicle_information_management_system.dto.VehicleWithDriver;
import com.vehicle_information_management_system.mapper.TripInfoMapper;
<<<<<<< HEAD
import com.vehicle_information_management_system.pojo.DriverInfo;
import com.vehicle_information_management_system.pojo.TripInfo;
=======
>>>>>>> 629a544 (update)
import com.vehicle_information_management_system.pojo.VehicleInfo;
import com.vehicle_information_management_system.service.VehicleInfoService;
import com.vehicle_information_management_system.mapper.VehicleInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

<<<<<<< HEAD
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
=======
import java.time.LocalDateTime;
import java.time.ZoneId;
>>>>>>> 629a544 (update)
import java.util.List;
import java.util.stream.Collectors;

/**
* @author huawei
* @description 针对表【vehicle_info】的数据库操作Service实现
* @createDate 2025-02-16 16:00:52
*/
@Service
@Slf4j
public class VehicleInfoServiceImpl extends ServiceImpl<VehicleInfoMapper, VehicleInfo>
    implements VehicleInfoService{

    @Autowired
    private VehicleInfoMapper vehicleInfoMapper;
    @Autowired
    private TripInfoMapper tripInfoMapper;
<<<<<<< HEAD
=======


>>>>>>> 629a544 (update)
    @Override
    public IPage<VehicleInfo> selectPage(Integer current, Integer size, String search) {
        Page<VehicleInfo> page = new Page<>(current,size);
        QueryWrapper<VehicleInfo> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(search)){
            queryWrapper.like("license_plate",search).or().like("manufacturer",search)
                    .or().like("production_date",search).or().like("load_capacity",search)
                    .or().like("dr.name",search);
        }
        autoReturnVehicle();
        return vehicleInfoMapper.getVehicleInfoWithDriver(page,queryWrapper);
    }

    @Override
    public void add(VehicleInfo vehicleInfo) {
        vehicleInfoMapper.insert(vehicleInfo);
    }

    @Override
    public void delete(Integer id) {
        vehicleInfoMapper.deleteById(id);
    }

    @Override
    public void update(VehicleInfo vehicleInfo) {
        vehicleInfoMapper.updateVehicleInfo(vehicleInfo);
    }

    @Override
    public List<VehicleCount> getVehicleInfoByDepartment() {
        return vehicleInfoMapper.getVehicleInfoByDepartment();
    }

    @Override
    public List<VehicleDto> getVehicleInfo() {
//        QueryWrapper<VehicleInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("vehicle_id","license_plate");
        return vehicleInfoMapper.getVehicleInfo();
    }

    @Override
    public VehicleWithDriver getVehicleWithDriver(Integer vehicleId)  {
        return vehicleInfoMapper.getDriverByVehicle(vehicleId);
    }

    @Override
    public List<VehicleCount> getVehicleCountByManufacturer() {
        QueryWrapper<VehicleCount> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("manufacturer","count(*) as vehicleCount").groupBy("manufacturer");
        return vehicleInfoMapper.selectVehicleCountByManufacturer(queryWrapper);
    }

    @Override
    public Integer getVehicleCount() {
        return Math.toIntExact(vehicleInfoMapper.selectCount(new QueryWrapper<VehicleInfo>().eq("1", 1)));
    }

    @Override
    public Integer getUseStatusVehicleCount() {
        return vehicleInfoMapper.getUseStatusVehicleCount();
    }

    @Override
    public List<VehicleDto> getNoUseVehicleInfo() {
        return vehicleInfoMapper.getNoUseVehicleInfo();
    }

    @Override
    public void autoReturnVehicle() {
        List<TripDto> tripInfos = tripInfoMapper.selectAllTrip();

        List<Integer> vehicleIdsToUpdate = tripInfos.stream()
                .filter(tripInfo -> tripInfo.getReturnTime() != null && tripInfo.getReturnTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().isBefore(LocalDateTime.now()))
                .map(TripDto::getVehicleId)
                .collect(Collectors.toList());
        log.info("vehicleIdsToUpdate:{}",vehicleIdsToUpdate);
        if (!vehicleIdsToUpdate.isEmpty()) {
            vehicleInfoMapper.batchUpdateUseStatus(vehicleIdsToUpdate);
            tripInfoMapper.batchUpdateIsProcessed(vehicleIdsToUpdate);
        }
    }

}




